package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Assets;
import light.head.model.AssetsAlteration;

import org.springframework.stereotype.Service;
@Service
public class AssetsDAO  extends BaseDAO<Assets, Integer>{
	
	private final static String sql_findApplyItem = " FROM AssetsAlteration ad WHERE ad.ac = ?";
	
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb){
		if(ocs == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM Assets g WHERE 1=1 AND g.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer ig : ocs) {
			hql.append("?,");
			list.add(ig);
		}
		
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String name = map.get(Parm.NAME);
			String eName = map.get(Parm.ENAME);
			String oc = map.get(Parm.OC);
			String deleted = map.get(Parm.DELETED);
			String gtc = map.get(Parm.GTC);
			String sort = map.get(Parm.SORT);
			
			if(isNotNull(name)){
				hql.append(" AND g.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(eName)){
				hql.append(" AND g.eName LIKE ?");
				list.add("%" + eName + "%");
			}if(isNotNull(oc)){
				hql.append(" AND g.oc = ?");
				list.add(Integer.parseInt(oc));
			}if (isNotNull(deleted)) {
				hql.append(" AND g.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}if(isNotNull(gtc)){
				hql.append(" AND g.gtc = ?");
				list.add(Integer.parseInt(gtc));
			}if(isNotNull(sort)){
				hql.append(" AND g.sort = ?");
				list.add(Byte.parseByte(sort));
			}
		}
		hql.append(" ORDER BY g.code DESC");
		
		return findToPageBean(hql.toString(), pb, list);
	}
	
	public List<AssetsAlteration> findByAc(Integer ac){
		if (null == ac)return null;
		List<AssetsAlteration> list = find(sql_findApplyItem, ac);
		return list;
	}
}
